package com.example.office.controller;

import com.example.office.util.http.HttpClientTools;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author jujh
 * @Date 2018/6/2
 */
@Controller
public class TestController {

    @RequestMapping(value = "/test-map.html")
    public String testMap(Model model) {
        System.out.println("test----map");
        model.addAttribute("name", "seawater");
        return "test";
    }

    @ResponseBody
    @RequestMapping(value = "/protal")
    public String getWeJsTicket(HttpServletRequest request) {
        System.out.println(request.getParameter("signature"));
        System.out.println(request.getParameter("echostr"));
        System.out.println(request.getParameter("timestamp"));
        System.out.println(request.getParameter("nonce"));
        //获取request所有信息
        showParams(request);
        return request.getParameter("echostr");
    }


    @RequestMapping(value = "get-access-token")
    public String getAccessToken() {
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        String reqUrl = url.replaceAll("APPID", this.appid).replaceAll("APPSECRET", this.secret);
        String resp = HttpClientTools.doGet(reqUrl);
        System.out.println(resp);
        if (StringUtils.isNotEmpty(resp)) {
            JSONObject jsob = JSONObject.fromObject(resp);
            String accessToken = jsob.getString("access_token");
            return accessToken;
        }
        return null;
    }

    @RequestMapping(value = "/create-menu")
    public String createMenu(HttpServletRequest request) {
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        String reqUrl = url.replaceAll("APPID", this.appid).replaceAll("APPSECRET", this.secret);
        String resp = HttpClientTools.doGet(reqUrl);
        System.out.println(resp);
        if (StringUtils.isNotEmpty(resp)) {
            JSONObject jsob = JSONObject.fromObject(resp);
            String accessToken = jsob.getString("access_token");
            HttpClientTools.doPostJson("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken, this.memuStr);
        }
        return "";
    }


    public static String deleteMenu() {
        String accessToken = "11_EgQxp4epefZyL0TqRHUdPxlTbGVWkQVyBp3gl2qqrLcY8_5iC7ypj6mRrsMUFkfQI1KWWkTSQL4AjascS0kbr-_BARTVJ56R4Df8_Cco32U74wvTaK9pklFTvGbggyAXTffsogpFDqOFMUjLXCReAGASXM";
        String reqUrl = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken;
        String resp = HttpClientTools.doGet(reqUrl);
        System.out.println(resp);
        return resp;
    }

    @RequestMapping(value = "/take-taxi")
    public String takeTaxi() {
        System.out.println("我要打车。。。。");
        return "taxi/take-taxi";
    }


    @RequestMapping(value = "/poi")
    public String search() {
        System.out.println("我要打车。。。。");
        return "taxi/poi";
    }

    @RequestMapping(value = "/zuobiao")
    public String zuobiao() {
        System.out.println("zuobiao。。。。");
        return "taxi/zuobiao";
    }

    @RequestMapping(value = "/wxpay")
    public String wxpay() {
        System.out.println("wxpay。。。。");
        return "taxi/wxpay";
    }

    @RequestMapping(value = "/coor")
    public String coor() {
        System.out.println("定位。。。。");
        return "taxi/coor";
    }

    @RequestMapping(value = "/test-netty")
    public void netty(String s) {
        System.out.println("s。。。。" + s);
        //return "taxi/coor";
    }


    public static void main1(String[] args) {
        System.out.println(deleteMenu());
    }

    public static void main(String[] args) {
        String menuStr = "{\n" +
                "     \"button\":[\n" +
                "     {\n" +
                "            \"name\": \"发送位置\", \n" +
                "            \"type\": \"location_select\", \n" +
                "            \"key\": \"rselfmenu_2_0\"\n" +
                "        },\n" +
                "\t  {    \n" +
                "\t\t  \"type\":\"view\",\n" +
                "          \"name\":\"我要打车\",\n" +
                "          \"url\":\"http://2131791w0j.iask.in/take-taxi\"\n" +
                "      }]\n" +
                " }";
        String resp = HttpClientTools.doPostJson("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=11_Z42MlQpnOU4azBwqTh4ofaqYk7I35XOylXw_Z_aMRZ3mv7VFbkQUuNPj0IBvzT_Yy481Lo9kCezsqKzbzZpc8eFa6Fn1N0whz4eMnYgFbLSOPhxCXwsarl7dF7R3fQHtQzYS2RzqYuu0xKmXSEZiAIADPK", menuStr);
        System.out.println(resp);
    }

    @Value("${wx.appid}")
    private String appid;
    @Value("${wx_secret}")
    private String secret;

    private String memuStr = "{\n" +
            "    \"button\": [ \n" +
            "        {\n" +
            "            \"name\": \"发图\", \n" +
            "            \"sub_button\": [\n" +
            "                {\n" +
            "                    \"type\": \"pic_sysphoto\", \n" +
            "                    \"name\": \"系统拍照发图\", \n" +
            "                    \"key\": \"rselfmenu_1_0\", \n" +
            "                   \"sub_button\": [ ]\n" +
            "                 }, \n" +
            "                {\n" +
            "                    \"type\": \"pic_photo_or_album\", \n" +
            "                    \"name\": \"拍照或者相册发图\", \n" +
            "                    \"key\": \"rselfmenu_1_1\", \n" +
            "                    \"sub_button\": [ ]\n" +
            "                }, \n" +
            "                {\n" +
            "                    \"type\": \"pic_weixin\", \n" +
            "                    \"name\": \"微信相册发图\", \n" +
            "                    \"key\": \"rselfmenu_1_2\", \n" +
            "                    \"sub_button\": [ ]\n" +
            "                }\n" +
            "            ]\n" +
            "        }, \n" +
            "        {\n" +
            "            \"name\": \"发送位置\", \n" +
            "            \"type\": \"location_select\", \n" +
            "            \"key\": \"rselfmenu_2_0\"\n" +
            "        }\n" +
            "    ]\n" +
            "}";


    /**
     * 打印request中所有数据
     *
     * @param request
     */
    private void showParams(HttpServletRequest request) {
        Map map = new HashMap();
        Enumeration paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() != 0) {
                    map.put(paramName, paramValue);
                }
            }
        }
        Set<Map.Entry<String, String>> set = map.entrySet();
        System.out.println("------------------------------");
        for (Map.Entry entry : set) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        System.out.println("------------------------------");
    }

}
